$(document).ready(function(){
	/**
	 * 获取列表页面（行为）按钮
	 */
	var authCondition = {
			menuCode:curMenu.code,
			drawerCode:行为类型_前端组件,
			authAreaCode:授权分类_定时任务管理_列表,
			callback:action,
			con:"#datagrid-toolbar"}
	findResourcesByUserId(authCondition);
	search();
});

/**
 * 查询
 */
function search(){
	$.ajax({
		type : "GET",
		url : ctx + "/schedule/query",
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			$("#datagrid").datagrid({
				border:false,
				fitColumns:true,
				checkbox:true,
				fit:true,
				remoteSort:false,
				rownumbers:true,
				singleSelect:true,
				toolbar: "#datagrid-toolbar",
				pagination:false,
				data: data.content
			});
		}
	});
}

/**
 * 添加
 */
function add(){
	$('#dialog').dialog({
	    title: '添加',
	    width: 600,
	    height: 400,
	    left:250,
	    top:60,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/schedule/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#cron").attr("href",ctx + "/web/cron/index.htm");
	    	$("#addform").form('load',{});
	    	//getDrawer('1020000000000000',setType);
	    	//getDrawer('1021000000000000',setStatus);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	var data = $("#addform").serializeJson();
            	$.ajax({
            		type : "POST",
            		url : ctx + "/schedule/create",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"添加成功")){
            				return;
            			}
            			
            			search();
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 添加
 */
function edit(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '修改',
	    width: 600,
	    height: 400,
	    left:250,
	    top:60,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/schedule/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#cron").attr("href",ctx + "/cron/index.htm");
	    	//getDrawer('1020000000000000',setType,setTypeValue);
	    	//getDrawer('1021000000000000',setStatus,setStatusValue);
	    	$("#addform").form('load',row);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	var data = $("#addform").serializeJson();
            	$.ajax({
            		type : "POST",
            		url : ctx + "/schedule/create",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"修改成功")){
            				return;
            			}
            			
            			search();
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 获取定时器抽屉
 * @param pid
 * @param callback
 */
function getDrawer(pid,callback,callback_){
	$.ajax({
		type : "GET",
		url : ctx + "/drawer/findByParentCode",
		dataType : "json",
		data : {pcode:pid},
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			if(typeof(callback) == 'function'){
				callback(data);
			}
			
			if(typeof(callback_) == 'function'){
				callback_();
			}
		}
	});
}

/**
 * 设置启动类型
 * @param data
 */
function setType(data){
	$("#type").combobox({
        valueField:'id',
        textField:'text',
        width:100,
		data:data
	});
}

/**
 * 修改时的类型值
 */
function setTypeValue(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$("#type").combobox('setValue',row.type);
}

/**
 * 删除任务
 */
function del(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	$.messager.confirm('提示', '确定要删除吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/schedule/delete",
	    		data :{name : row.name,group : row.group},
	    		dataType : "json",
	    		success : function(data) {
	    			if(!showMsg(data,"删除成功")){
        				return;
        			}
	    			search();
	    		}
	    	});
	    }
	});
}

/**
 * 启动任务,立即执行一次
 */
function execute(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	$.messager.confirm('提示', '确定要执行一次吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/schedule/execute",
	    		data :{name : row.name,group : row.group},
	    		dataType : "json",
	    		success : function(data) {
	    			if(!showMsg(data,"执行成功")){
        				return;
        			}
	    			//search();
	    		}
	    	});
	    }
	});
}

/**
 * 暂停任务
 */
function pause(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	if(row.status=='PAUSED'){
		message("已暂停");
		return;
	}
	
	$.messager.confirm('提示', '确定要暂停吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/schedule/pause",
	    		data :{name : row.name,group : row.group},
	    		dataType : "json",
	    		success : function(data) {
	    			if(!showMsg(data,"暂停成功")){
        				return;
        			}
	    			search();
	    		}
	    	});
	    }
	});
}

/**
 * 启动任务
 */
function start(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	if(row.status=='NORMAL'){
		message("已启动");
		return;
	}
	
	$.messager.confirm('提示', '确定要启动吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/schedule/resume",
	    		data :{name : row.name,group : row.group},
	    		dataType : "json",
	    		success : function(data) {
	    			if(!showMsg(data,"启动成功")){
        				return;
        			}
	    			search();
	    		}
	    	});
	    }
	});
}

/**
 * 添加事件
 *//*
$('#datagrid-toolbar #add').linkbutton({
	onClick:function(){
		add();
	}
});

*//**
 * 修改事件
 *//*
$('#datagrid-toolbar #edit').linkbutton({
	onClick:function(){
		edit();
	}
});

*//**
 * 删除事件
 *//*
$('#datagrid-toolbar #del').linkbutton({
	onClick:function(){
		del();
	}
});

*//**
 * 启动事件
 *//*
$('#datagrid-toolbar #start').linkbutton({
	onClick:function(){
		start();
	}
});

*//**
 * 暂停事件
 *//*
$('#datagrid-toolbar #pause').linkbutton({
	onClick:function(){
		pause();
	}
});

*//**
 * 恢复事件
 *//*
$('#datagrid-toolbar #execute').linkbutton({
	onClick:function(){
		execute();
	}
});*/

